package com.mooc.apigateway.utils;

import com.google.common.io.Files;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.Instant;

public class FileUtil {
  
  
  public static File saveToLocal(MultipartFile file, String filePath) throws IOException {
     //file.getOriginalFilename() 不同浏览器返回不同，谷歌浏览器返回文件名，ie浏览器返回路径
      String originalName = file.getOriginalFilename();
      String fileName = "";
      if(originalName.contains("\\")){
          fileName= originalName.substring(originalName.lastIndexOf("\\") +1);
      }else{
          fileName = originalName;
      }


    File newfile =   new File(filePath+"/"+Instant.now().getEpochSecond()+"/"+fileName);
    if (!newfile.exists()) {
         newfile.getParentFile().mkdirs();
         newfile.createNewFile();
    }
    Files.write(file.getBytes(), newfile);
    return newfile;
  }

    public static void main(String[] args) {
        String aa ="C:\\Users\\ZWZHENG\\Desktop\\timg.jpg";
        String bb = aa.substring(aa.lastIndexOf("\\") +1);
        System.out.println("bb:" + bb);
    }



}
